package pt.iul.poo.balloons;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Point;
import java.io.IOException;
import java.util.LinkedList;

import pt.iul.poo.game.Player;
import pt.iul.poo.image.MovingImage;
import pt.iul.poo.image.properties.DisplayableElement;
import pt.iul.poo.image.properties.GameWindowInterface;
import pt.iul.poo.terrain.Floor;

public abstract class Balloon {
	private int health;
	private Color color;
	private int PRIORITY;
	private Player player;
	private int positionX;
	private int positionY;
	private int value = 0;
	private MovingImage image;
	
	public Balloon(String fileName, int x, int y, int PRIORITY) throws IOException {
		image= new MovingImage(fileName, x, y, PRIORITY);
		this.setPositionX(x);
		this.setPositionY(y);
		this.PRIORITY= PRIORITY;
	}

	public int getHealth() {
		return health;
	}

	public void setHealth(int health) {
		this.health = health;
	}

	public Color getColor() {
		return color;
	}

	public void setColor(Color color) {
		this.color = color;
	}

	public boolean moveBallon(GameWindowInterface window,
			LinkedList<Balloon> ballons, LinkedList<Floor> listOfPositions,
			Dimension nSquares, Player player) {

		Point nextPosition = generateNextPosition(listOfPositions, window);
		if (comparePositionOutsideWindow(nextPosition, nSquares, window)) {
			player.setHealth(player.getHealth() - 1);
			return true;
		} else {
			this.move((nextPosition.x - getPositionX()),
					(nextPosition.y - getPositionY()));
			this.setPositionX(nextPosition.x);
			this.setPositionY(nextPosition.y);
			this.update();
			return false;
		}
	}

	private void move(int i, int j) {
		image.move(i, j);
		
	}

	private boolean comparePositionOutsideWindow(Point nextPosition,
			Dimension nSquares, GameWindowInterface window) {
		if (nSquares.width * window.getDefaultElementWidth() < nextPosition.x
				|| nSquares.height * window.getDefaultElementHeight() < nextPosition.y) {
			return true;
		}
		return false;
	}

	public Point generateNextPosition(LinkedList<Floor> listOfPositions,
			GameWindowInterface window) {
		int xAtual = getPositionX();
		int yAtual = getPositionY();

		yAtual = yAtual + window.getDefaultElementHeight();
		if (free(xAtual, yAtual, listOfPositions)) {
			return new Point(xAtual, yAtual);
		} else {
			yAtual = yAtual - window.getDefaultElementHeight();
			return new Point(xAtual = xAtual + window.getDefaultElementWidth(),
					yAtual);
		}
	}

	private boolean free(int xAtual, int yAtual,
			LinkedList<Floor> listOfPositions) {
		int xFloor = -1;
		int yFloor = -1;
		for (int i = 0; i < listOfPositions.size(); i++) {
			xFloor = listOfPositions.get(i).getPosition().x;
			yFloor = listOfPositions.get(i).getPosition().y;
			if (xFloor == xAtual && yFloor == yAtual) {
				return false;
			}
		}
		return true;
	}

	public int getPositionX() {
		return positionX;
	}

	public void setPositionX(int positionX) {
		this.positionX = positionX;
	}

	public int getPositionY() {
		return positionY;
	}

	public void setPositionY(int positionY) {
		this.positionY = positionY;
	}

	public Player getPlayer() {
		return player;
	}

	public void setPlayer(Player player) {
		this.player = player;
	}

	public int getValue() {
		return value;
	}

	public void setValue(int value) {
		this.value = value;
	}

	public void update() {
		image.update();
		
	}

	public Point getPosition() {
		return image.getPosition();
	}

	public Dimension getDimension() {
		return image.getDimension();
	}

	public DisplayableElement getImage() {
		return image;
	}
	
}